package com.synjones.ui.check;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.synjones.entry.SignPerson;
import com.synjones.idcardusb_reader.R;
import com.synjones.ui.FirstScreenRouter;

/**
 * A simple {@link Fragment} subclass.
 */
public class CheckFragment extends Fragment implements CheckContract.View{

    public static final int DO_SIGN = 0;
    public static final int DO_PRINT = 1;
    private static final String ARG_PARAM1 = "invitationCode";
    private static final String ARG_PARAM2 = "doWhat";
    private static final String ARG_PARAM3 = "person";

    private CheckContract.Presenter presenter;

    private FirstScreenRouter main;

    private String invitationCode;

    private SignPerson person;

    private int doWhat = 0;

    private Button checkSign;
    private Button checkCancel;
    private ImageView printSechduleYes;
    private ImageView printSechduleNo;
    private View printSchedule;

    private TextView name;
    private TextView work;
    private TextView hotel;
    private TextView room;
    private TextView sex;


    public CheckFragment() { }


    public static CheckFragment newInstance(String invitationCode, int doWhat,SignPerson person) {
        CheckFragment fragment = new CheckFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, invitationCode);
        args.putInt(ARG_PARAM2, doWhat);
        args.putParcelable(ARG_PARAM3, person);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            invitationCode = getArguments().getString(ARG_PARAM1);
            doWhat = getArguments().getInt(ARG_PARAM2);
            person = getArguments().getParcelable(ARG_PARAM3);
        }
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_check, container, false);
        checkSign = (Button) view.findViewById(R.id.checkSign);
        checkCancel = (Button) view.findViewById(R.id.checkCancel);
        printSechduleYes = (ImageView) view.findViewById(R.id.printScheduleYes);
        printSechduleNo = (ImageView) view.findViewById(R.id.printScheduleNo);
        printSchedule = view.findViewById(R.id.checkPrintSchedule);
        name = (TextView) view.findViewById(R.id.checkName);
        work = (TextView) view.findViewById(R.id.checkWork);
        sex = (TextView) view.findViewById(R.id.checkSex);
        room = (TextView) view.findViewById(R.id.checkRoom);
        hotel = (TextView) view.findViewById(R.id.checkHotel);
        setView();
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        main = (FirstScreenRouter) getActivity();
        presenter = new CheckPresenter(this, main,person);
    }

    private void setView(){
        if (person!=null){
            name.setText(person.getName());
            work.setText(person.getWorkPlace());
            sex.setText(person.getSex());
            room.setText(person.getRoomNumber());
            hotel.setText(person.getHotelName());
        }
        if (doWhat == DO_SIGN){
            checkSign.setVisibility(View.VISIBLE);
            printSchedule.setVisibility(View.GONE);
        }else {
            checkSign.setVisibility(View.GONE);
            printSchedule.setVisibility(View.VISIBLE);
        }
        checkSign.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                presenter.Sign(invitationCode,person.getDocumentNumber());
            }
        });
        checkCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                main.gotoWelcome();
            }
        });
    }

    @Override
    public void showBackInfo(String msg, int color) {
        main.showToast(msg,color);
    }
}
